﻿using AutoMapper;
using Gecko.Entities;
using Gecko.Models;
using Gecko.Models.Dto;
using Gecko.Respositories;
using Gecko.Util;
using log4net;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gecko.Services
{
    public class WaterLeakResourceService : IDisposable
    {
        ILog _log = LogManager.GetLogger("Gecko.Services");
        private ISession _session;
        private WaterLeakResourceRepository _resourceRepository;
        private WaterLeakRepository _waterLeakRepository;
        public WaterLeakResourceService()
        {
            _session = NHibernateHelper.OpenSession();
            _resourceRepository = new WaterLeakResourceRepository(_session);
            _waterLeakRepository = new WaterLeakRepository(_session);
        }

        public void AddWaterLeakResource(int waterLeakId, IList<WaterLeakResourceModel> resources){

            var waterLeak = _waterLeakRepository.Get(waterLeakId);
            
            if (waterLeak != null)
            {
                var tx = _session.BeginTransaction();
                try
                {
                    foreach (var item in resources)
                    {
                        var waterLeakResource = new WaterLeakResource
                        {
                            MediaType = item.MediaType,
                            Resource = item.Resource,
                            Size = item.Size
                        };
                        waterLeak.Resources.Add(waterLeakResource);
                        _session.Persist(waterLeakResource);
                    }
                    tx.Commit();
                }
                catch (Exception ex)
                {
                    if (tx.IsActive)
                    {
                        tx.Rollback();
                    }
                    _log.Error(ex);
                }
            }
            else
            {
                throw new Exception("Not found waterLeak");
            }
        }

        public void Dispose()
        {
            _session.Close();
        }

        public ServiceResponse<IList<WaterLeakResourceDto>> GetByWaterLeakId(int waterLeakId)
        {
            var response = new ServiceResponse<IList<WaterLeakResourceDto>>();

            try
            {
                //http://localhost:59354/api/leakcontent/11
                

                var waterLeak = _waterLeakRepository.Get(waterLeakId);
                if (waterLeak != null)
                {
                    response.Data = Mapper.Map<IList<WaterLeakResourceDto>>(waterLeak.Resources);
                    response.Message = "Success";
                    response.Page = 1;
                    response.PageSize = 100;
                    response.Status = true;
                    response.Total = waterLeak.Resources!=null?waterLeak.Resources.Count:0;
                }
                else
                {
                    throw new Exception("Not found waterLeak");
                }



                
            }
            catch (Exception ex)
            {

                response.Status = false;
                response.Message = ex.Message;
                _log.Error(ex.Message, ex);
            }
            return response;

            
        }

        public WaterLeakResource GetImage(int id)
        {
            return _resourceRepository.Get(id);
        }
    }
}